# -*- coding: utf-8 -*-
import scrapy
from  scrapy.http import Request,FormRequest
from douban.items import DoubanItem
from urllib import request

class DoubSpider(scrapy.Spider):
    name = 'doub'
    allowed_domains = ['douban.com']
    # start_urls = ['http://douban.com/']
    header = {
        "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.1 Safari/605.1.15"}
    def start_requests(self):
        return [Request("https://accounts.douban.com/login",headers=self.header,callback=self.parse,meta={"cookiejar":1})]

    def parse(self, response):
        captcha = response.xpath('//img[@id="captcha_image"]/@src').extract()
        if len(captcha) > 0:
            print("此时有验证码" + captcha[0])
            localpath = "/Users/delin/Desktop/ScrapyDevelopDemo/douban/douban/captchaImage/captcha.png"
            request.urlretrieve(captcha[0], filename=localpath)
            print("请查看本地验证码图片，并输入验证码")
            captcha_value = input()

            data = {
                "form_email": "2352915584@qq.com",
                "form_password": "1023lin1023",
                "redir": "https://www.douban.com/people/180852249/",
                "captcha-solution":captcha_value,
            }
            print("有验证码登录中.....")

        else:
            print("此时没有验证码")
            data = {
                "form_email": "2352915584@qq.com",
                "form_password": "1023lin1023",
                "redir": "https://www.douban.com/people/180852249/",
            }
            print("无验证码登录中.....")
        print("开始爬取")
        return [FormRequest.from_response(response,
                                          meta={"cookiejar": response.meta["cookiejar"]},
                                          headers=self.header,
                                          formdata=data,
                                          callback=self.next,
                                          )]  # [] 固定格式



    def next(self,response):
        print("此时已经登录完成并爬取了个人中心的数据")
        title = response.xpath("/html/head/title/text()").extract()
        content = response.xpath("//div[@class='note']/p/text()").extract()
        print(title[0])
        print(content[0])